package cn.tom.tompicturebackend.websocket;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.tom.tompicturebackend.model.entity.Picture;
import cn.tom.tompicturebackend.model.entity.Space;
import cn.tom.tompicturebackend.model.entity.User;
import cn.tom.tompicturebackend.model.enums.SpaceTypeEnum;
import cn.tom.tompicturebackend.service.PictureService;
import cn.tom.tompicturebackend.service.SpaceService;
import cn.tom.tompicturebackend.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class WsHandshakeInterceptor implements HandshakeInterceptor {

    @Resource
    private UserService userService;

    @Resource
    private PictureService pictureService;

    @Resource
    private SpaceService spaceService;
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        if(request instanceof ServletServerHttpRequest) {
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String pictureId = servletRequest.getParameter("pictureId");
            if(StrUtil.isBlank(pictureId)) {
                log.error("pictureId is null");
                return false;

            }
            User loinUser = userService.getLoinUser(servletRequest);
            if(ObjUtil.isEmpty(loinUser)) {
                log.error("loinUser is null");
                return false;

            }
            Picture picture = pictureService.getById(pictureId);

            if(ObjUtil.isEmpty(picture)) {
                log.error("picture is null");
                return false;
            }
            Long spaceId = picture.getSpaceId();
            Space space = null;
            if (spaceId !=null){
                space = spaceService.getById(spaceId);
                if (space ==null){
                    log.error("space is null");
                    return false;
                }
                if (space.getSpaceType() != SpaceTypeEnum.TEAM.getValue()){
                    log.error("space is not team");
                    return false;
                }
            }
            List<String> permissionList = spaceUserAuthManager.getPermissionList(space,loinUser);

            attributes.put("user",loinUser);
            attributes.put("userId",loinUser.getId());
            attributes.put("pictureId",Long.valueOf(pictureId));

        }

        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}
